<?php
/**
 * Ora - 后台 其他管理模块
 * @author lan7
 * @copyright lan7.net
 * @version 2.0
 */

if(!defined('IN_ORA')) {
	exit('Access Denied');
}
class admin_other extends mod {
	/**
	 * 初始化
	 *
	 * @return admin_other
	 */
	function admin_other(){
		parent::__construct();
		session_start();
		$this->setTplDir(PUBLIC_PATH);
		$this->setMsgTpl("admin/msg.html");
		if(!isset($_SESSION[$GLOBALS['ora_cfg']['sess_prefix']."admin"])){
			$this->showMsg("请先登录!",parse_uri("common-login"),1);
		}
	}

	/**
	 * 网站信息配置
	 *
	 */
	function info(){
		if(isset($_POST['submit'])){
			$inf_id 	=	$_POST['inf_id'];
			$inf_name 	= 	clear_html($_POST['inf_name']);
			$inf_qq		=	clear_html($_POST['inf_qq']);
			$inf_mail	=	clear_html($_POST['inf_mail']);
			$inf_zip	=	clear_html($_POST['inf_zip']);
			$inf_phone	=	clear_html($_POST['inf_phone']);
			$inf_cellphone=	clear_html($_POST['inf_cellphone']);
			$inf_address=	clear_html($_POST['inf_address']);
			$inf_icp	=	clear_html($_POST['inf_icp']);
			$inf_copy	=	clear_html($_POST['inf_copy']);
			if(intval($inf_id)>0){
				$sql = "update ".tn('info')." set inf_name='$inf_name',inf_qq='$inf_qq',inf_mail='$inf_mail',inf_zip='$inf_zip'";
				$sql.= ",inf_phone='$inf_phone',inf_cellphone='$inf_cellphone',inf_address='$inf_address',inf_icp='$inf_icp',";
				$sql.=" inf_copy='$inf_copy' where Id = $inf_id";
			}else{
				$sql = "insert into ".tn('info')." values(default,'$inf_name','$inf_qq','$inf_mail','$inf_zip','$inf_phone','$inf_cellphone','$inf_address','$inf_icp','$inf_copy')";
			}
			if($GLOBALS['db']->execute($sql)){
				$this->showMsg("更新成功!",parse_uri('admin_other-info'),true);
			}
		}
		$GLOBALS['tpl']->assign('info',$GLOBALS['db']->getOne('select * from '.tn('info').' order by Id asc limit 1'));
		$GLOBALS['tpl']->display('admin/info.html');
	}

	/**
	 * 网站导航管理
	 *
	 */
	function nav(){
		$do = empty($GLOBALS['_param']['do'])?'list':$GLOBALS['_param']['do'];
		$GLOBALS['tpl']->assign('do',$do);
		//导航列表
		if($do == 'list'){
			$GLOBALS['tpl']->assign('nav',$GLOBALS['db']->getAll('select * from '.tn('nav').' order by nav_sort desc'));
		}
		//添加导航
		else if($do == 'add'){
			$GLOBALS['tpl']->assign('cats',$GLOBALS['db']->getAll('select * from '.tn('cat').' where pid = 0 order by sort desc'));
			$GLOBALS['tpl']->assign('nav',$GLOBALS['db']->getAll('select * from '.tn('pages').' where is_nav = 1 order by Id desc'));
		}
		//删除导航
		else if($do == 'del'){
			$id = $GLOBALS['_param']['id'];
			if(is_numeric($id)){
				$GLOBALS['db']->execute("delete from ".tn('nav')." where Id = $id");
			}
			$this->showMsg('操作成功!',parse_uri('admin_other-nav'),1);
		}
		//新增导航
		else if($do == 'save'){
			$nav_title = $_POST['nav_title'];
			$nav_url   = $_POST['nav_url'];
			$nav_target= $_POST['nav_target']?1:0;
			if(empty($nav_title)|$nav_title==''){
				$this->showMsg('请输入导航名称!',$_SERVER['HTTP_REFERER'],1);
			}
			if(empty($nav_url)|$nav_url == ''){
				$this->showMsg('请输入导航地址!',$_SERVER['HTTP_REFERER'],1);
			}

			if(preg_match('/^(http)|(www).*?$/i',$nav_url)){
				$sql = "insert into ".tn("nav")."(nav_title,nav_url,nav_sort,nav_target) ";
				$sql.= "values('$nav_title','$nav_url',0,$nav_target)";
			}else{
				$sql = "insert into ".tn("nav")."(nav_title,nav_code,nav_sort,nav_target) ";
				$sql.= "values('$nav_title','$nav_url',0,$nav_target)";
			}

			if($GLOBALS['db']->execute($sql)){
				$this->showMsg('操作成功!',parse_uri('admin_other-nav'),1);
			}else{
				$this->showMsg('操作失败了!',parse_uri('admin_other-nav'),1);
			}
		}
		//输出模板页
		$GLOBALS['tpl']->display("admin/nav.html");
	}

	/**
	 * 排序更新
	 *
	 */
	function ajax(){
		$this->setIsAjax(true);
		$do = $GLOBALS['_param']['do'];
		if($do == 'navsort'){
			$id = $GLOBALS['_param']['id'];
			$val= $GLOBALS['_param']['val'];
			if(is_numeric($id)){
				$id = intval($id);
				$val = intval($val);
				$sql = 'update '.tn("nav").' set nav_sort = '.$val.' where id = '.$id;
				$GLOBALS['db']->execute($sql);
				$this->showMsg('250');
			}
		}
		else if($do == 'catsort'){
			$id = $GLOBALS['_param']['id'];
			$val= $GLOBALS['_param']['val'];
			if(is_numeric($id)){
				$id = intval($id);
				$val = intval($val);
				$sql = 'update '.tn("cat").' set sort = '.$val.' where id = '.$id;
				$GLOBALS['db']->execute($sql);
				$this->showMsg('250');
			}
		}
	}

	/**
	 * 标签主题管理
	 * @todo 主题聚合
	 *
	 */
	function tags(){
		$do = empty($GLOBALS['_param']['do'])?'list':$GLOBALS['_param']['do'];
		$GLOBALS['tpl']->assign('do',$do);
		if($do == 'check'){
			$sql = "select * from ".tn('tags')."  group by tag_name order by Id desc ";
			$GLOBALS['tpl']->assign('tags',$GLOBALS['db']->getAll($sql));
			$GLOBALS['tpl']->assign('page_title','标签列表');
		}
		$tags = array();
		$tags["art"] = array();
		$tags["pro"] = array();
		$tagNames = $GLOBALS['db']->getAll("select tag_name from ".tn("tags")." group by tag_name order by Id desc ");
		foreach($tagNames as $v){
			$sql = "select * from ".tn("tags")." where tag_name = '{$v['tag_name']}'";
			$tmp = $GLOBALS['db']->getAll($sql);
			for ($i=0;$i<sizeof($tmp);$i++){
				if($tmp[$i]["tag_type"] == 1){//Pro
					$pro = $GLOBALS['db']->getOne("select pro_name from ".tn("product")." where Id = ".$tmp[$i]["fk_id"]);
					$tmp[$i]["title"] = $pro["pro_name"];
					$tags["pro"][$tmp[$i]["Id"]]  = $tmp[$i];
				}else{
					$art = $GLOBALS['db']->getOne("select title from ".tn("article")." where Id = ".$tmp[$i]["fk_id"]);
					$tmp[$i]["title"] = $art["title"];
					$tags["art"][$tmp[$i]["Id"]]  = $tmp[$i];
				}
			}
		}
		$GLOBALS['tpl']->assign("full_tags",$tags);
		$GLOBALS['tpl']->display('admin/tags.html');
	}

	/**
	 * 产品属性模板管理
	 *
	 */
	function ptpl(){
		$do = isset($GLOBALS['_param']['do'])?$GLOBALS['_param']['do']:'list';
		$GLOBALS['tpl']->assign("do",$do);
		if($do == 'list'){
			$GLOBALS['tpl']->assign("pts",$GLOBALS['db']->getAll("select * from ".tn("ptpl")." order by Id desc"));
		}
		else if($do == 'modify' || $do == 'add'){
			$id = $GLOBALS['_param']['id'];
			if(is_numeric($id)){
				$ptpl = $GLOBALS['db']->getOne("select * from ".tn("ptpl")."  where Id = $id ");
				if($ptpl){
					$ptpl['tpl'] = unserialize($ptpl['tpl']);
					$GLOBALS['tpl']->assign("ptpl",$ptpl);
				}else{
					$this->showMsg("无此记录!");
				}
			}
		}
		else if($do == 'save'){
			$attr = $_POST["attr"];
			$attr = @array_filter($attr,"loseEmpty");
			if(empty($attr)){
				$this->showMsg("请添加属性!",$_SERVER["HTTP_REFERER"],1);
			}
			$tplstr = serialize($attr);
			$cname = clear_html($_POST["cname"]);
			if($cname==""){
				$this->showMsg("请输入模板名称!",$_SERVER["HTTP_REFERER"],1);
			}
			$id = $_POST['Id'];
			if(is_numeric($id)&&$id>0){
				$sql = "update ".tn("ptpl")." set cname = '$cname' , tpl = '$tplstr' where Id = $id";
			}else{
				$sql = "insert into ".tn("ptpl")." values(default,'$cname','$tplstr')";
			}
			$GLOBALS['db']->execute($sql);
			$this->showMsg("操作成功!",parse_uri("admin_other-ptpl"),1);
		}
		else if($do == 'del'){
			$id = intval($GLOBALS['_param']['id']);
			if($id>0){
				if($this->db->execute("delete from ".tn('ptpl')." where Id = $id")){
					$this->showMsg("删除成功!",parse_uri("admin_other-ptpl"),1);
				}
			}
		}
		$GLOBALS['tpl']->display("admin/ptpl.html");
	}
}

/**
	 * 去除空字符串
	 *
	 * @param unknown_type $var
	 * @return unknown
	 */

function loseEmpty($var){
	return !(empty($var)&&$var == "");
}
?>